<template>
  <div class="system-user-dialog-container">
    <el-dialog title="修改状态" v-model="state.dialog.isShowDialog" width="500">
      <el-form ref="formRef" :model="state.ruleForm" :rules="state.rules" size="default" label-width="70px">
        <el-form-item label="状态" prop="status">
          <CommonSelect class="w100" :options="contractOptions" v-model="state.ruleForm.status" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onCancel" size="default">取 消</el-button>
          <el-button type="primary" @click="onSubmit" size="default" :loading="state.submit.loading">提交</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="UpdateStatusDialog">
import { reactive, ref } from 'vue';
import { updateStatus } from '/@/api/contract';
import { ElMessage } from 'element-plus';
import CommonSelect from '/@/components/common-select/index.vue';
import { Contract } from '/@/models';
import { useOptions } from '/@/hooks/use-options';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

const contractOptions = useOptions('ContractStatus');

// 定义变量内容
const formRef = ref();
const state = reactive({
  id: '',
  ruleForm: {
    status: '',
  },
  rules: {
    status: [{ required: true, message: '不能为空' }],
  },
  submit: {
    loading: false,
  },
  dialog: {
    isShowDialog: false,
    type: '',
    title: '',
  },
});

// 打开弹窗
const openDialog = (row: Contract) => {
  state.id = row.id
  state.ruleForm.status = ''
  state.dialog.isShowDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
  state.dialog.isShowDialog = false;
};

// 取消
const onCancel = () => {
  closeDialog();
};

// 提交
const onSubmit = async () => {
  await formRef.value.validate();
  try {
    state.submit.loading = true;
    await updateStatus(state.id, state.ruleForm)
    ElMessage.success('提交成功');
    closeDialog();
    emit('refresh');
  } catch (err) {
    console.log(err);
  }

  state.submit.loading = false;
};

// 暴露变量
defineExpose({
  openDialog,
  closeDialog,
});
</script>
